package kino.database;

import kino.object.KinoObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Kino数据库
 */
public class KinoDB {

    /**
     *  数据库编号
     */
    private Integer dbNo;
    /**
     *  数据库存储字典
     */
    private Map<String, KinoObject> dict;
    /**
     *  键过期时间存储表
     */
    private Map<String, Long> expires;
    public KinoDB(Integer dbNo) {
        this.dbNo = dbNo;
        this.dict = new HashMap<>(128);
        this.expires = new HashMap<>(128);
    }
    public Map<String, KinoObject> getDict() {
        return dict;
    }

    public void setDict(Map<String, KinoObject> dict) {
        this.dict = dict;
    }

    public Integer getDbNo() {
        return dbNo;
    }

    public void setDbNo(Integer dbNo) {
        this.dbNo = dbNo;
    }
    public boolean isExpire(String key) {
        if (this.expires.containsKey(key)) {
            return this.expires.get(key) < System.currentTimeMillis();
        }
        return false;
    }

    public void removeExpire(String key) {
        this.dict.remove(key);
        this.expires.remove(key);
    }
    public void removeExpireAndClearKey(String key) {
        this.dict.remove(key);
        this.expires.remove(key);
    }

    public void setExpire(String key,Long time) {
        expires.put(key, time);
    }
    public Long getExpire(String key) {
        return expires.get(key);
    }
}
